package com.tz.system.menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.tz.platform.utils.StringUtil;
import com.tz.system.menu.model.TSysMenu;
import com.tz.system.user.UserUtils;
import com.tz.system.user.model.UserInfo;

@Service
public class MenuUtils {

	/**
	 * makeMenusTree:(菜单树). 
	 * @author changhz
	 * @param request
	 * @return
	 */
	public String makeMenusTree(HttpServletRequest request) {
		StringBuffer urlAll = request.getRequestURL(); // 全路径  http://localhost/platform/system/menu/tree.do
		String platformPath = request.getContextPath();
		String[] pathArr = urlAll.toString().split(platformPath);
		String basePath = pathArr[0] + platformPath;
		StringBuffer strMenuStr = new StringBuffer();
		// 1、获取当前登录用户信息
		UserInfo userinfo = UserUtils.getSessionUser(request);
		List<TSysMenu> menus = userinfo.getMenus();
		for (TSysMenu sysMenu : menus) {
			strMenuStr.append("<li>");
			if (sysMenu.getMenuLevel() == 1) {
				strMenuStr.append("<a href=\"#\"><i class=\"fa " + sysMenu.getCssClass() + "\"></i>");
				strMenuStr.append("<span class=\"nav-label\">" + sysMenu.getMenuName() + "</span>");
				strMenuStr.append("<span class=\"fa arrow\"></span></a>");
				String secondStr = makeSecondMenus(menus, sysMenu.getMenuCode(), basePath);
				if(!StringUtil.msIsEmpty( secondStr )){
					strMenuStr.append(secondStr);
				}
			} 
			strMenuStr.append("</li>");
		}
		System.out.println(strMenuStr.toString());
		return strMenuStr.toString();
	}
	
	/**
	 * makeSecondMenus:(第二层菜单). 
	 * @author changhz
	 * @param menus
	 * @param menuCode
	 * @param basePath
	 * @return
	 */
	public String makeSecondMenus(List<TSysMenu> menus, String menuCode, String basePath){
		StringBuffer strMenuStr = new StringBuffer("");
		for (TSysMenu sysMenu : menus) {
			if (sysMenu.getMenuLevel() == 2 && menuCode.equals(sysMenu.getUpMenuCode()) ) {
				String thirdStr = makeThirdMenus(menus, sysMenu.getMenuCode(), basePath);
				if(!StringUtil.msIsEmpty( thirdStr )){
					strMenuStr.append("<li>");
					strMenuStr.append("<a href=\"#\">" + sysMenu.getMenuName() + "<span class=\"fa arrow\"></span></a>");
					strMenuStr.append( thirdStr );
					strMenuStr.append("</li>");
				}else{
					strMenuStr.append("<li>");
					strMenuStr.append("<a class=\"J_menuItem\" href=\"" + basePath + sysMenu.getMenuPage() + "\">");
					strMenuStr.append(sysMenu.getMenuName());
					strMenuStr.append("</a></li>");
				}
			}
		}
		String secondStr = "";
		if(!StringUtil.msIsEmpty(strMenuStr.toString())){
			secondStr = "<ul class=\"nav nav-second-level\">" + strMenuStr.toString() + "</ul>";
		}
		return secondStr;
	}
	
	/**
	 * makeThirdMenus:(第三层菜单). 
	 * @author changhz
	 * @param menus
	 * @param menuCode
	 * @param basePath
	 * @return
	 */
	public String makeThirdMenus(List<TSysMenu> menus, String menuCode, String basePath){
		StringBuffer strMenuStr = new StringBuffer("");
		for (TSysMenu sysMenu : menus) {
			if (sysMenu.getMenuLevel() == 3 && menuCode.equals(sysMenu.getUpMenuCode())) {
				strMenuStr.append("<li>");
				strMenuStr.append("<a class=\"J_menuItem\" href=\"" + basePath + sysMenu.getMenuPage() + "\">");
				strMenuStr.append(sysMenu.getMenuName());
				strMenuStr.append("</a></li>");
			}
		}
		String thirdStr = "";
		if(!StringUtil.msIsEmpty(strMenuStr.toString())){
			thirdStr = "<ul class=\"nav nav-third-level\">" + strMenuStr.toString() + "</ul>";
		}
		return thirdStr;
	}
	

}
